﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Member
{
    public partial class AddPointFrm : Form
    {
        MySqlServer mysqlserver = new MySqlServer();
        private int point = 0;
        private string card_id;
        private DateTime expireDate;
        Member.VipType vipType = Member.VipType.Normal;
        public bool updateOk = false;
        public AddPointFrm(string _cardId, string name, int _curPoint, int _vipType, DateTime _expireDate)
        {
            InitializeComponent();
            point = _curPoint;
            card_id = _cardId;
            expireDate = _expireDate;
            vipType = (Member.VipType)_vipType;

            this.ActiveControl = tbPoint;
            lbName.Text += name;
            lbCardId.Text += _cardId;
            tbPoint.KeyPress += tbPoint_KeyPress;
        }

        private void addPoint()
        {
            if (tbPoint.Text != "" )
            {
                int bonus = int.Parse(tbPoint.Text);
                if (bonus <= 0)
                {
                    MessageBox.Show("Points number must greater than 0");
                    return;
                }

                int rowAffect = 0;
                int nextPoint = point + bonus;
                int VIP_6M_POINT = 180;
                int VIP_1_YEAR_POINT = 360;
                if (vipType == Member.VipType.Normal && point <= VIP_6M_POINT && nextPoint > VIP_6M_POINT && nextPoint < VIP_1_YEAR_POINT)
                {
                    if (expireDate > DateTime.Now.AddMonths(6))
                        vipType = Member.VipType.Vip6Month;
                }
                else if (vipType != Member.VipType.Vip1Year && vipType != Member.VipType.VipForever && point < VIP_1_YEAR_POINT && nextPoint >= VIP_1_YEAR_POINT)
                {
                    if(expireDate.AddDays(1) > DateTime.Now) //tinh den cuoi ngay thay vi dau ngay
                        vipType = Member.VipType.Vip1Year;
                }

                if (vipType == Member.VipType.Vip6Month)
                {
                    string expire_date = DateTime.Now.AddMonths(6).Year + "/" + DateTime.Now.AddMonths(6).Month + "/" + DateTime.Now.AddMonths(6).Day;
                    rowAffect = mysqlserver.ExecuteNonQuery("Update CUSTOMER Set point='" + nextPoint + "', is_vip='" + (int)vipType + "', expire_date='" + expire_date + "' Where card_id='" + card_id + "'");
                }
                else if (vipType == Member.VipType.Vip1Year)
                {
                    DateTime newExpire = expireDate.AddMonths(6); //cong don them 6 thang
                    string expire_date = newExpire.Year + "/" + newExpire.Month + "/" + newExpire.Day;
                    rowAffect = mysqlserver.ExecuteNonQuery("Update CUSTOMER Set point='" + nextPoint + "', is_vip='" + (int)vipType + "', expire_date='" + expire_date + "' Where card_id='" + card_id + "'");
                }
                else
                {
                    rowAffect = mysqlserver.ExecuteNonQuery("Update CUSTOMER Set point='" + nextPoint + "' Where card_id='" + card_id + "'");
                }
                if (rowAffect > 0)
                {
                    updateOk = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Add point Unsuccessful");
                }
            }
            else
            {
                MessageBox.Show("Please input points number");
            }
        }

        void tbPoint_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            if ((Keys)e.KeyChar == Keys.Enter)
                addPoint();
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            addPoint();
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
